﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using fqlmoney.DAL;
using fqlmoney.Common;
using fqlmoney.Model;
using System.Data;

namespace fqlmoney.BLL
{
    public class OrderBLL
    {

        OrderDAL dal = new OrderDAL();

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="extsql">sql条件</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageCount">条数</param>
        /// <param name="totalPages">总页数</param>
        /// <param name="totalItems">总条数</param>
        /// <returns></returns>
        public List<user_order> GetPage(string extsql, int pageIndex, int pageCount, ref int totalPages, ref int totalItems)
        {
            return dal.GetPage(extsql, " o_time desc ", pageIndex, pageCount, ref totalPages, ref totalItems);
        }

        /// <summary>
        /// 获取扇形图数据
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public List<dynamic> GetPieData(int uid, string date,int type)
        {
            string extsql = "";
            if (type==0)
            {
                extsql = string.Format(" and user_order.`u_id`={0} and user_order.o_state=2 AND o_time LIKE'{1}%'  ", uid, date);
            }
            else
            {
                extsql = string.Format(" and user_order.`u_id`={0} and user_order.o_state=2 and user_order.t_id={2} AND o_time LIKE'{1}%'  ", uid, date, type);
            }

            return dal.GetPieData(extsql);
        }

        /// <summary>
        /// 添加账单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Add(user_order model)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                int count = dal.Add(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "新增成功";
                }
                else
                {
                    rm.code = 701;
                    rm.msg = "新增失败:请联系管理员";
                }
            }
            catch (Exception e)
            {
                rm.code = 500;
                rm.msg = "服务器错误";
                LogHelper.Error(this.GetType().FullName, e.ToString());
            }
            return rm;
        }

        /// <summary>
        /// 更新账单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Update(user_order model)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                int count = dal.Update(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "更新成功";
                }
                else
                {
                    rm.code = 701;
                    rm.msg = "更新失败:请联系管理员";
                }
            }
            catch (Exception e)
            {
                rm.code = 500;
                rm.msg = "服务器错误";
                LogHelper.Error(this.GetType().FullName, e.ToString());
            }
            return rm;
        }

        /// <summary>
        /// 删除账单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public ReturnMessages Delete(int id, int uid)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                int count = dal.Delete(id, uid);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "删除成功";
                }
                else
                {
                    rm.code = 701;
                    rm.msg = "删除失败:请联系管理员";
                }
            }
            catch (Exception e)
            {
                rm.code = 500;
                rm.msg = "服务器错误";
                LogHelper.Error(this.GetType().FullName, e.ToString());
            }
            return rm;
        }

    }
}
